//
//  ITMoreSettingView.m
//  InTubePro
//
//  Created by seven on 2020/2/5.
//  Copyright © 2020 seven. All rights reserved.
//

#import "ITMoreSettingView.h"

#import "ITWebViewController.h"

#import "ITGeneralManager.h"

@interface ITMoreSettingView(){
    
}
IT_PROPERTY_STRONG ITBaseTipView *showView;
@end

@implementation ITMoreSettingView

+ (instancetype)showWithType:(int)type{
    UIView *superView = [AppUtils findRootController].view;
    ITMoreSettingView *view = [[self alloc] initWithFrame:superView.bounds type:type];
    [view showInView:superView];
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame type:(int)type{
    self = [super initWithFrame:frame];
    if (self) {
        self.items = NSMutableArray.array;
        self.type = type;
        [self initViews];
    }
    return self;
}

//0详情设置 1首页e设置 2收藏列表 3用户主页 4普通食品列表  5举报  6设置
- (void)setType:(int)type{
    _type = type;
    
    NSArray *tmp = nil;
    if (_type <= 1) {
//        if (ITShareManager.shouldShowShareVideo){
//            [_items addObject:[ITSettingModel modelWithType:ITSettingTypeVideoShare]];
//        }
    }
    if (self.type == 0) {
        tmp = @[
            [ITSettingModel modelWithType:ITSettingTypeReport],
            [ITSettingModel modelWithType:ITSettingTypeBlock],
            [ITSettingModel modelWithType:ITSettingTypeSpeed],
            [ITSettingModel modelWithType:ITSettingTypeCancel],
        ];
    }else if (self.type == 1){
        NSArray *tmp = @[
            [ITSettingModel modelWithType:ITSettingTypeReport],
            [ITSettingModel modelWithType:ITSettingTypeDislike],
            [ITSettingModel modelWithType:ITSettingTypeBlock],
            [ITSettingModel modelWithType:ITSettingTypeCancel],
        ];
        [_items addObjectsFromArray:tmp];
    }else if (self.type == 2){
        tmp = @[
            [ITSettingModel modelWithType:ITSettingTypeDislike],
            [ITSettingModel modelWithType:ITSettingTypeCancel],
        ];
    }else if (self.type == 3){
        tmp = @[
            [ITSettingModel modelWithType:ITSettingTypeReport],
            [ITSettingModel modelWithType:ITSettingTypeBlock],
            [ITSettingModel modelWithType:ITSettingTypeCancel],
        ];
    }else if (self.type == 4){
        tmp = @[
            [ITSettingModel modelWithType:ITSettingTypeReport],
            [ITSettingModel modelWithType:ITSettingTypeCancel],
        ];
    }else if(self.type == 5){
        tmp = @[
            LocalTitle(@"Vulgar Pornography"),
            LocalTitle(@"Title Party"),
            LocalTitle(@"Incorrect Content"),
            LocalTitle(@"Repetition of Old News"),
            LocalTitle(@"Garbage Content"),
        ];
    }else if (self.type == 6){
        tmp = @[
            [ITSettingModel modelWithType:ITSettingTypeSetting],
            //[ITSettingModel modelWithType:ITSettingTypeFeedback],
            [ITSettingModel modelWithType:ITSettingTypeFavorites],
            [ITSettingModel modelWithType:ITSettingTypeProfile],
            [ITSettingModel modelWithType:ITSettingTypeCancel],
        ];
    }else if(self.type == 7){
        tmp = @[
            LocalTitle(@"Camera"),
            LocalTitle(@"Photo album"),
            //LocalTitle(@"Cancel"),
        ];
    }
    [_items addObjectsFromArray:tmp];
}

- (void)initViews{
    [super initViews];
    self.alpha = 0;
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    
    if (_itemRect.size.width == 0) {
        self.itemRect = CGRectMake(0, 5, self.width, 50);
    }
    
    CGFloat height = _items.count * _itemRect.size.height + _itemRect.origin.y;
    if (!ITOrientationManager.sharedInsatnce.supportLandspcape) {
        height += [UIDevice safeAreaBottomHeight] + (IPHONE_X ? 10 : 20);
    }else {
        height += (IPHONE_X ? 34 : 10);
    }
    _contentView = [BaseView initWithFrame:CGRectMake((self.width-kScreenWidth)/2, self.height, kScreenWidth, height)];
    [_contentView setCornerRadius:16];
    _contentView.backgroundColor = ColorWithHex(0x1A1A1D);
    [self addSubview:_contentView];
    
    CGRect rect = _itemRect;
    for (int i = 0; i<_items.count; i++) {
        ITSettingModel *model = _items[i];
        UIButton *btn = [self itemWithModel:model frame:rect];
        if (btn.tag == 0) {
            btn.tag = 101+i;
        }
        rect.origin.y += btn.height;
    }
}

- (UIButton *)itemWithModel:(ITSettingModel *)model frame:(CGRect)frame{
    UIButton *btn = [UIButton initWithFrame:frame];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.titleLabel.font = MediumFontWithSize(16);
    [btn setTitleColor:COLOR_WHITE];
    if ([model isKindOfClass:ITSettingModel.class]) {
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 65, 0, 0);
        UIImage *img = [[UIImage imageNamed:model.icon] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
        UIImageView *iv = [UIImageView initWithFrame:CGRectMake(19, (btn.height-22)/2, 22, 22)];
        iv.contentMode = UIViewContentModeCenter;
        iv.image = img;
        if (model.type == ITSettingTypeCancel) {
            [btn addLineWithFrame:CGRectMake(0, 0, btn.width, 0.5) color:COLOR_CUSTOM_LINE];
            iv.tintColor = [UIColor colorWithWhite:1 alpha:0.5];
        }else {
            if (self.type == 6) {
                iv.tintColor = COLOR_WHITE;
            }else {
                iv.tintColor = [UIColor colorWithWhite:1 alpha:0.5];
            }
        }
        
        [btn addSubview:iv];
    
        //[btn setImage:img];
        [btn setTitleNormal:model.title];
        btn.tag = 100+model.type;
        //btn.tintColor = [UIColor colorWithWhite:1 alpha:0.5];
    }else if ([model isKindOfClass:NSString.class]){
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 25, 0, 0);
        [btn setTitleNormal:(NSString *)model];
    }
    [btn addTarget:self action:@selector(onBtnClick:)];

    [_contentView addSubview:btn];
    return btn;
}

- (void)onBtnClick:(UIButton *)sender{
    [self hideAnimation];
    
    switch (sender.tag-100) {
        
        case ITSettingTypeBlock:{
            [self onBlock];
        }
            break;
        case ITSettingTypeDislike:{
            if (self.customAction) {
                self.customAction(self, cat_dislike);
            }
        }
            break;
        case ITSettingTypeVideoShare:{
            
            if (self.customAction) {
                self.customAction(self, cat_share);
            }
            /*
            ITVideoShareView *view = [ITVideoShareView initWithFrame:self.frame];
            view.customAction = self.customAction;
            [view showInView:self.superview];
            
            [self hideAnimation];
             //*/
        }
            break;
        case ITSettingTypeReport:{
            
            _showView.customAction = self.customAction;
        }
            break;
        case ITSettingTypeSpeed:{
            [self hideAnimation];
        }
            break;
        case ITSettingTypeCancel:{
            [self hideAnimation];
        }
            break;
        
        case ITSettingTypeSetting:{
            
        }
            break;
        case ITSettingTypeFeedback:{
            
        }
            break;
        case ITSettingTypeFavorites:{
            
        }
            break;
        case ITSettingTypeProfile:{
            
        }
            break;
        case ITSettingTypeShare:{
            
        }
            break;
            
        default:
            break;
    }
}

- (void)hideAllViews{
    [self hideAnimation];
    if (_showView) {
        [_showView hideAnimation];
        self.showView = nil;
    }
    
}

- (void)showChanged{
    [super showChanged];
    _contentView.bottom = self.height+20;
}

- (void)hideChanged{
    [super hideChanged];
    _contentView.top = self.height;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self hideAnimation];
}



- (void)onBlock{
    WEAKSELF
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:LocalTitle(@"Block user") message:LocalTitle(@"Blocking this user will not see his content.") preferredStyle:(UIAlertControllerStyleAlert)];
    [alert addAction:[UIAlertAction actionWithTitle:LocalTitle(@"cancel") style:(UIAlertActionStyleCancel) handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:LocalTitle(@"block") style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        if (self.customAction) {
            self.customAction(weak_self, cat_block);
        }
    }]];
    [[AppUtils findRootController] presentViewController:alert animated:YES completion:nil];
}


@end
